home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / btoc_v12.lha / BtoCDir / ExplodeStuff / GCC / Explode.c < prev   
Encoding:
C/C++ Source or Header  |  1994-04-27  |  2.6 KB  |  113 lines

  1. #define PROGRAM_NAME    "Explode"
  2. #define VERSION        "v3.0"
  3.  
  4. /* Explode is to be used together with Decrunch3x.c */
  5.  
  6. /* This version of Explode uses Decrunch30.c to explode  */
  7. /* data compressed with BtoC v3.0. Use it as an example. */
  8.  
  9. /* Sorry, no makefile.                    */
  10. /* To compile and link (without optimization):        */
  11. /* gcc explode.c decrunch30.c -o Explode -lc -lamiga    */
  12.  
  13. #include <exec/types.h>
  14. #include <exec/memory.h>
  15. #include <libraries/dos.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19.  
  20. #include <inline/exec.h>
  21. #include <inline/dos.h>
  22.  
  23. #define BTOC_MSG    ( (ULONG)'B'<<24L | (ULONG)'T'<<16L | 'O'<<8 | 'C' )
  24.  
  25. char *NO_MEM = "No memory.";
  26. char *USAGE = "\x1B[33m"PROGRAM_NAME"\x1B[0m "VERSION
  27. " - to test the compression algorytmh of BtoC "VERSION
  28. "\nBy \x1B[3m\x1B[2mStefano Reksten \x1B[0mof 3AM - \x1B[1mThe Three Amigos!!!\x1B[0m\n"
  29. "Usage: "PROGRAM_NAME" <file.CMPR>";
  30.  
  31.  
  32. ULONG data_size;
  33. BPTR handle;
  34. UBYTE *expl_ptr, *cmpr_ptr;
  35. UBYTE most_used;
  36. BOOL verbose=FALSE;
  37. char filename[128];
  38.  
  39. extern BOOL Decrunch30(UBYTE *,UBYTE *);
  40.  
  41. void Fail(char*);
  42. int main(int,char**);
  43.  
  44.  
  45. void Fail( char *s )
  46. {
  47. if ( s )    puts( s );
  48. if ( handle )    Close( handle );
  49. if ( expl_ptr )    FreeMem( expl_ptr, data_size );
  50. if ( cmpr_ptr )    FreeMem( cmpr_ptr, data_size );
  51.  
  52. exit( 0 );
  53. }
  54.  
  55.  
  56. int main( int argc, char **argv )
  57. {
  58. if ( argc==1 )
  59.     Fail( USAGE );
  60.  
  61. puts( PROGRAM_NAME );
  62.  
  63. strcpy( filename, argv[1+verbose] );
  64. printf( "File choosen : %s\n", filename );
  65.  
  66. if ( !( handle=Open( filename, MODE_OLDFILE ) ) )
  67.     Fail( "No file" );
  68.  
  69. Read( handle, &data_size, sizeof(ULONG) );
  70. if ( data_size != BTOC_MSG )
  71.     Fail( "Data NOT compressed with BtoC" );
  72.  
  73. Read( handle, &data_size, sizeof(ULONG) );
  74. Read( handle, &most_used, sizeof(UBYTE) );
  75.  
  76. if ( !(expl_ptr=(UBYTE *)AllocMem( data_size, MEMF_PUBLIC ) ) )
  77.     Fail( NO_MEM );
  78.  
  79. if ( !(cmpr_ptr=(UBYTE *)AllocMem( data_size, MEMF_PUBLIC ) ) )
  80.     Fail( NO_MEM );
  81.  
  82. /* Actually, you don't need to Alloc "data_size" bytes, as you    */
  83. /* compressed your data. You should know the exact size of the    */
  84. /* compressed file. (Lock, Examine & Alloc, or WRITE IT ON A    */
  85. /* PIECE OF PAPER!!!)                        */
  86. /* Doing THIS way is a waste of memory.                */
  87.  
  88.  
  89. Seek( handle, 0, OFFSET_BEGINNING );
  90. Read( handle, cmpr_ptr, data_size );
  91. Close( handle ); handle = NULL;
  92.  
  93. printf( "Now decrunching.\n" );
  94.  
  95. if ( Decrunch30( expl_ptr, cmpr_ptr ) )
  96.     {
  97.     printf( "Done, writing .EXPL file.\n" );
  98.  
  99.     if ( !stricmp( filename+strlen( filename )-4, ".CBM" ) )
  100.         filename[ strlen( filename )-4 ] = NULL;
  101.  
  102.     strcat( filename, ".EXPL" );
  103.  
  104.     if ( !(handle = Open( filename, MODE_NEWFILE ) ) )
  105.         Fail( "File not opened" );
  106.  
  107.     Write( handle, expl_ptr, data_size );
  108.  
  109.     Fail( "File exploded." );
  110.     }
  111. else    Fail( "Did NOT decrunch!" );
  112. }
  113.